<?php

function getLocationIntId($BDD_connexion,$locationId)
{
	
	$request = "SELECT bien.ref_intermediaire FROM bien,location WHERE location.id_location = :id_location AND location.ref_bien = bien.id_bien";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(':id_location',$locationId,PDO::PARAM_INT);
	$prepare->execute();
	$id = $prepare->fetch(PDO::FETCH_OBJ);
	
	return $id->ref_intermediaire;
	
}

function changeLocationPers($BDD_connexion,$locationId,$newPers)
{
	
	$request = "UPDATE location SET ref_personne = :newPers WHERE id_location = :id";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(':newPers',$newPers,PDO::PARAM_INT);
	$prepare->bindValue(':id',$locationId,PDO::PARAM_INT);
	
	return $prepare->execute();
	
}

function GetLocationInformations($BDD_connexion,$id) {
	
	$request_count = "SELECT * FROM bien, adresse WHERE id_bien = :id AND adresse.id_adresse = bien.ref_adresse";
	$prepare=$BDD_connexion->prepare($request_count);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->execute();
	$bien = $prepare->fetch(PDO::FETCH_OBJ);
	$bien->description = getLocationDescription($BDD_connexion,$id);
	
	return $bien;
			
}

function GetProprioInformations($BDD_connexion,$id) {
	
	$request_count = "SELECT * FROM personne WHERE (SELECT ref_proprio FROM bien WHERE id_bien = :id) = personne.id";
	$prepare=$BDD_connexion->prepare($request_count);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->execute();
	$proprio = $prepare->fetch(PDO::FETCH_OBJ);
	
	return $proprio;
	
}

function GetLocationsByGood($BDD_connexion,$id_bien) {
	
	$locations = array();
	
	$request = "SELECT * FROM location, personne WHERE location.ref_bien = :id_bien AND personne.id = location.ref_personne AND DATEDIFF(date_debut,CURRENT_DATE()) > 0";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(':id_bien',$id_bien,PDO::PARAM_INT);
	$prepare->execute();
	
	while ($location = $prepare->fetch(PDO::FETCH_OBJ))
	{
		
		$locations[] = $location;
		
	}
	
	return $locations;
	
}

function GetLocationByPers($BDD_connexion,$id) {
	
	$locations = array();
	
	$request = "SELECT * FROM location WHERE ref_personne = :id";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->execute();
	while ($location = $prepare->fetch(PDO::FETCH_OBJ))
	{
		$location->bien = GetLocationInformations($BDD_connexion,$location->ref_bien);
		$location->proprio = GetProprioInformations($BDD_connexion,$location->ref_bien);
		$location->commentaire = GetComByUser($BDD_connexion,$location->ref_bien,$_SESSION['userid']);
		$locations[] = $location;
	}
	
	return $locations;
	
}

function GetComByUser($BDD_connexion,$goodId,$userId)
{
	
	$coms = array();
	
	$requestCom = "SELECT * FROM avis_utilisateur WHERE id_bien = :id_bien AND id_personne = :id_user";
	$prepareCom = $BDD_connexion->prepare($requestCom);
	$prepareCom->bindValue(':id_bien',$goodId,PDO::PARAM_INT);
	$prepareCom->bindValue(':id_user',$userId,PDO::PARAM_INT);
	$prepareCom->execute();
	$advice = $prepareCom->fetch(PDO::FETCH_OBJ);
	
	return $advice;
	
}

function CountDifferentsAdvices($advices) {
	
	$advicesCount = array(	"pros" => 0,
							"cons" => 0	);
	
	
	foreach ($advices as $advice)
	{
		if ($advice->notation >= 5)
		{
			$advicesCount["pros"]++;
		}
		else
		{
			$advicesCount["cons"]++;
		}
	}
	
	return $advicesCount;
	
}

?>
